﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.Windows.Forms;
using ThematicClustering.RasterMap;
using ThematicClustering.Utils;
using ThematicClustering.Algorithm;
using System.ComponentModel;

namespace ThematicClustering.GUI
{
    /// <summary>
    /// A felhődetektáló program főablak osztálya.
    /// </summary>
    public partial class MainForm : Form
    {
        #region Fields

        private Map rasterMap;
        private Point lastMousePos;
        private Boolean movementMode;

        #region ImageLayers

        ImageLayers Layers;
        ImageLayers Pattern;
        ImageLayers Result;

        #endregion


        #endregion

        #region Constructors

        /// <summary>
        /// Grafikus indítási mód konstruktora.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            navigatorControl.Enabled = false;
        }

        #endregion

        #region Event Handlers

        #region Menu Event Handlers

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void eredetiKepToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Filter = "Tagged Images(*.tif, *.tiff) | *.tif;*.tiff";
            ofd.Title = "Eredeti kép betöltése";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Bitmap picture;

                try
                {
                    picture = new Bitmap(ofd.FileName);
                }
                catch (Exception)
                {
                    MessageBox.Show("Nem sikerült megnyitnom a fájlt! Próbálja újra.");
                    return;
                }
                Layers = new ImageLayers(picture);

                try
                {
                    SetRasterMapOnMapPictureBox(picture);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                
                navigatorControl.Enabled = true;
                referenciaKepToolStripMenuItem.Enabled = true;
            }
        }

        private void referenciaKepToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Filter = "Tagged Images(*.tif, *.tiff) | *.tif;*.tiff";
            ofd.Title = "Referencia kép betöltése";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Bitmap picture;

                try
                {
                    picture = new Bitmap(ofd.FileName);
                }
                catch (Exception)
                {
                    MessageBox.Show("Nem sikerült megnyitnom a fájlt! Próbálja újra.");
                    return;
                }
                Pattern = new ImageLayers(picture);
                Pattern.MaskedStats(Layers);
                chiButton.Enabled = true;
            }
        }

        private void saveResultToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Tagged Images(*.tif, *.tiff)|*.tif; *.tiff";
            sfd.Title = "Eredmény mentése";
            sfd.DefaultExt = "tif";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                Bitmap resultPic = Result.CreateImage();
                resultPic.Save(sfd.FileName, ImageFormat.Tiff);
            }
        }

        #endregion

        #region Mouse Event Handlers on PictureBox

        void mapPictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (rasterMap != null && movementMode)
            {
                rasterMap.Move(lastMousePos.X - e.X, lastMousePos.Y - e.Y);
                lastMousePos = e.Location;
            }
        }

        void mapPictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            mapPictureBox.Cursor = System.Windows.Forms.Cursors.Arrow;
            movementMode = false;
        }

        void mapPictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            mapPictureBox.Cursor = System.Windows.Forms.Cursors.SizeAll;
            movementMode = true;
            lastMousePos = e.Location;
        }

        #endregion

        private void chiButton_Click(object sender, EventArgs e)
        {
            Result = AppAlgorithms.ChiSquareProbation(Layers, Pattern.Nu, Pattern.Covariance);
            SetRasterMapOnMapPictureBox(Result.CreateImage());
            saveResultToolStripMenuItem.Enabled = true;
        }

        #endregion

        #region Member Methods

        /// <summary>
        /// Megjelenítendő kép beállítása.
        /// </summary>
        /// <param name="map">A megjelenítendő kép.</param>
        private void SetRasterMapOnMapPictureBox(Bitmap map)
        {
            rasterMap = new Map(map);
            rasterMap.ZoomDisplay = mapPictureBox;
            navigatorControl.RasterMap = rasterMap;
        }

        #endregion
    }
}
